package cn.zyjblogs.server.authority.controller;

import cn.zyjblogs.server.authority.dto.PermissionResourcesDto;
import cn.zyjblogs.server.authority.service.PermissionResourcesService;
import cn.zyjblogs.server.authority.vo.PermissionResourcesVo;
import cn.zyjblogs.starter.common.entity.context.BaseContext;
import cn.zyjblogs.starter.common.entity.response.ResponseObject;
import cn.zyjblogs.starter.common.entity.response.ResponseResult;
import cn.zyjblogs.starter.web.apiversion.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhuyijun
 */
@Api(value = "权限资源控制层")
@RequestMapping("/{v}/menu")
@RestController
@ResponseBody
@ApiVersion(1)
@RequiredArgsConstructor
public class PermissionResourcesController {
    private final PermissionResourcesService permissionResourcesService;

    @ApiOperation(value = "新增权限组", notes = "新增权限组")
    @PutMapping("/save")
    @ApiVersion(1)
    public ResponseObject<Object> save(@Validated @RequestBody PermissionResourcesDto dto) {
        permissionResourcesService.save(dto);
        return ResponseResult.success();
    }


    @ApiOperation(value = "修改权限组", notes = "修改权限组")
    @PutMapping("/update")
    @ApiVersion(1)
    public ResponseObject<Object> update(@Validated @RequestBody PermissionResourcesDto dto) {
        permissionResourcesService.update(dto);
        return ResponseResult.success();
    }


    @ApiOperation(value = "删除权限组", notes = "删除权限组")
    @DeleteMapping("/delete")
    @ApiVersion(1)
    public ResponseObject<Object> deleteById(@RequestParam("id") String id) {
        permissionResourcesService.removeById(id);
        return ResponseResult.success();
    }


    @ApiOperation(value = "查询所有权限组", notes = "查询所有权限组")
    @GetMapping("/list/all")
    @ApiVersion(1)
    public ResponseObject<List<PermissionResourcesVo>> listAll() {
        return ResponseResult.success(permissionResourcesService.listVoInTenant());
    }


    @ApiOperation(value = "查询用户权限组", notes = "查询用户权限组")
    @GetMapping("/list/userId")
    @ApiVersion(1)
    public ResponseObject<List<PermissionResourcesVo>> listUser() {
        return ResponseResult.success(permissionResourcesService.listByUserId(BaseContext.getUserId()));
    }

    @ApiOperation(value = "查询用户权限组", notes = "查询用户权限组")
    @GetMapping("/list/roleId")
    @ApiVersion(1)
    public ResponseObject<List<PermissionResourcesVo>> listRoleId(String roleId) {
        return ResponseResult.success(permissionResourcesService.listRoleId(roleId));
    }

}
